/************************************************************************************/
/*  Program name CAPGLIMP_SECURITY_USAGE.sql                                        */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_SECURITY_USAGE';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_FLEX_VALUE_RULE_USAGE;
		
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO FND_FLEX_VALUE_RULE_USAGES
			VALUES
			(
			C1.application_id,
			CAPBRIMP_COMMON.GetRespID(C1.responsibility_id_name),
			CAPBRIMP_MAPPING.GetFNDValueRuleIDMapping(C1.flex_value_rule_id),
			C1.last_update_date,
			--CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.LAST_UPDATED_BY,
			C1.creation_date,
			--CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATED_BY,
			C1.last_update_login,
			CAPBRIMP_MAPPING.GetFNDValueSetIDMapping(C1.flex_value_set_id),
			C1.parent_flex_value_low,
			C1.parent_flex_value_high
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/